﻿using System;
using System.Windows;
using System.Windows.Input;
using Quizzer.Model;
using Quizzer.Helpers;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Quizzer.DataAccess;
using System.Data.SqlClient;
using System.Configuration;


namespace Quizzer.ViewModel
{
    [Magic]
    public class RegistrationViewModel : ViewModelBase
    {
        public RegistrationViewModel()
        {
            Registration = new RelayCommand(() => ExecuteRegistration(),
                ()=>( !(string.IsNullOrEmpty(Username)) && !(string.IsNullOrEmpty(Password)) ));

            ErrorUsernameVisible = Visibility.Collapsed;   // hide any error messages
            ErrorPasswordVisible = Visibility.Collapsed;
        }

        #region Properties

        public Visibility ErrorUsernameVisible { get; set; }
        public Visibility ErrorPasswordVisible { get; set; }

        public string Username { get; set; }
        public string Password { get; set; }
        
        #endregion

        #region Commands
        
        public ICommand Registration { get; private set; }
        private void ExecuteRegistration()
        {
            ErrorUsernameVisible = Visibility.Collapsed;   // hide any error messages
            ErrorPasswordVisible = Visibility.Collapsed;

            if (AccountRepository.ContainsUsername(Username))
            {
                ErrorUsernameVisible = Visibility.Visible;
                return;
            }

            //if (Password.Length < 6)
            //{
            //    ErrorPasswordVisible = Visibility.Visible;
            //    return;
            //}

            Account acc = AccountRepository.CreateAccount(Username, Password);

            //Messenger.Default.Send<ViewModelBase, MainViewModel>(new CoursesViewModel());
            
            Messenger.Default.Send<string, MainViewModel>("CoursesPage");
            Messenger.Default.Send<Account, CoursesViewModel>(acc);
        }

        #endregion
    }
}
